<?php

/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class Administrator_TopimageController extends Zend_Controller_Action
{
	

	public function init(){
		
		$member=new CMember();
		
		if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())){
			
			$this->_redirect(MY_URL.'/administrator-auth-login');
			
		}
		
	}
	
	public function preDispatch()
	{
		
		$this->getHelper ('layout')->setLayout('framelayout');
		
		$this->view->addHelperPath(MY_ROOT.'/vhelpers');
		
		Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');
		
		//$this->getHelper('User')->getinfo($this->view,$this->_request->getModuleName(),$this->_request->getControllerName());
		
	}
 

	public function editAction(){
		
		$imgString=file_get_contents(MY_ROOT.'/html/upload/turn_round/topimage.txt');
		
		$imgArray=array();
		
		$imgArray=json_decode($imgString,true);

		$this->view->assign('imgArray',$imgArray);
		
	}

	public function logosAction(){

		$imgString=file_get_contents(MY_ROOT.'/html/upload/turn_round/topimage.txt');

		$imgArray=array();

		$imgArray=json_decode($imgString,true);

		$this->view->assign('imgArray',$imgArray);

	}
 
	public function doeditAction(){
		
		$this->_helper->viewRenderer->setNoRender();
    	
    	$thisArray=$this->_request->getParams();
		
		$path=new CPath();
		
		$imgArray=array();
			
		for($i=0;$i<count($thisArray['imgUrl']);$i++){
			
			if($thisArray['nochange'][$i]!='1'){
		
				$new_url=$path->toDbPath('item',$thisArray['imgUrl'][$i]);
						
				CFile::copyAndUnlink($path->toAbsoluteRootPath($thisArray['imgUrl'][$i]),$path->toAbsoluteRootPath($new_url));
				
				$thisArray['imgUrl'][$i]=$new_url;
				
			}
			
			$imgArray['imgHref'][]=$thisArray['imgHref'][$i];
			
			$imgArray['imgUrl'][]=$thisArray['imgUrl'][$i];
			
			$imgArray['captionText'][]=$thisArray['captionText'][$i];
			$imgArray['captionText2'][]=$thisArray['captionText2'][$i];
			$imgArray['linkType'][]=$thisArray['linkType'][$i];
			$imgArray['linkTarget'][]=$thisArray['linkTarget'][$i];
			$imgArray['enableButtonWhenInFront'][]=$thisArray['enableButtonWhenInFront'][$i];
			
		}
		
		file_put_contents(MY_ROOT.'/html/upload/turn_round/topimage.txt',json_encode($imgArray));
		//xml write
 
		$xmlstr = '<?xml version="1.0" encoding="UTF-8"?><settings autoRotate="1" autoRotateSpeed="4" useSubtitle="0" useTooltip="1" useSecondCaption="1" spanX="330" spanY="60" centerX="500"  centerY="370" distanceValue="0" perspectiveRatio="1.5" minimumscale="0" turningspeed="4" rotationKind="1" useFocalBlur="1" focalBlurValue=".6" useMotionBlur="1"  motionBlurValue=".25" useFadeOnMouseOver="1" mouseOverAlphaValue=".5"  useReflection="1" reflectionAlphaValue=".5"/><photos>'; 
		for($i=0;$i<count($imgArray['imgUrl']);$i++){
			
			$xmlstr = $xmlstr."<photo ";
			$xmlstr = $xmlstr.' imageURL="'.$imgArray['imgUrl'][$i].'" '  ;
			$xmlstr = $xmlstr.' linkData="'.$imgArray['imgHref'][$i].'" ' ;
			$xmlstr = $xmlstr.' linkType="'.$imgArray['linkType'][$i].'" '  ;
			$xmlstr = $xmlstr.' linkTarget="'.$imgArray['linkTarget'][$i] .'" ' ;
			
			$xmlstr = $xmlstr.' captionText="'.$imgArray['captionText'][$i].'" '  ;
			$xmlstr = $xmlstr.' captionText2="'.$imgArray['captionText2'][$i].'" '  ;
			
			
			$xmlstr = $xmlstr.' enableButtonWhenInFront="'.$imgArray['enableButtonWhenInFront'][$i].'" '  ;
			$xmlstr = $xmlstr.' />';
			
		}
        $xmlstr = $xmlstr."</photos>";
file_put_contents(MY_ROOT.'/data.xml',$xmlstr);
		//xml write end
		$this->view->showMsg("更新成功！",$_SERVER['HTTP_REFERER']);
		
	}
  
        
	
    public function uploadimgAction(){
    	
    	header("Content-type: text/html; charset=utf-8");
    	
    	$this->getHelper ('layout')->disableLayout ();
    	  	
    	$upload=new CUpload();
    	
    	$path=new CPath();
    	
    	$thisPath=$path->getPathFromType('temp');
	
    	$imageArray=$upload->uploadFile($thisPath,null,null,'500000');
    	
    	$isSuccess=false;
    	
    	if(is_array($imageArray)){
    		
    		$isSuccess=true;
    		
	    	$imgUrl=$path->toDbPath('temp',$imageArray[0]);
	    	
	    	$this->view->assign('imgUrl',$imgUrl);
	    	
    	}else{
    		
    		$error=$imageArray;
    		
    		$this->view->assign('error',$error);
    		
    	}
    	
    	$this->view->assign('isSuccess',$isSuccess);	
    	
    }
    
	public function delimgAction(){
    	
    	$this->getHelper ('layout')->disableLayout ();
    	
    	$this->_helper->viewRenderer->setNoRender();
    	
    	if(@unlink(MY_ROOT.'/'.$this->_request->getParam('imgurl'))){
    		
    		echo 'success';
    		
    	}else{
    		
    		echo 'success';
    		
    	}
    	
    }

	
	public function postAction(){
	
		//$this->_helper->viewRenderer->setNoRender();
		
		//$this->getHelper ('layout')->disableLayout ();
	
	}
    

    
}
